/**
 * Copyright (c) 2012, http://www.yissugames.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yissugames.blocklife;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;

import com.yissugames.blocklife.gamelogic.Block;
import com.yissugames.blocklife.gamelogic.BlockCamera;
import com.yissugames.blocklife.gamelogic.World;
import com.yissugames.gui.Button;
import com.yissugames.gui.Event;
import com.yissugames.gui.GUI;

public class GameScreen implements Screen {

	private World world;
	private String seed;
	
	private boolean isSpoken;
	
	private boolean showESCmenu;
	
	private Button backToGame;
	private Button backToMenu;
	private Button craftingRecipes;
	
	public GameScreen(String seed)
	{
		this.seed = seed;
		Event e1 = new Event()
		{
			@Override
			public void onClick(GUI g) {
				showESCmenu = false;
			}
		};
		this.backToGame = new Button(Display.getWidth() / 2 - 250 / 2, Display.getHeight() / 2 - 50 / 2 - 23, 250, 36, "Back to Game", e1);
		
		Event e2 = new Event()
		{
			@Override
			public void onClick(GUI g) {
				ScreenSystem.set(new MainMenuScreen());
			}
		};
		this.backToMenu = new Button(Display.getWidth() / 2 - 250 / 2, Display.getHeight() / 2 - 50 / 2 + 23, 250, 36, "Back to Main Menu", e2);
	
		Event e3 = new Event()
		{
			@Override
			public void onClick(GUI g) {
				ScreenSystem.setAndSave(new CraftingRecipesScreen());
			}
		};
		this.craftingRecipes = new Button(Display.getWidth() - 260, Display.getHeight() - 46, 250, 36, "Crafting Recipes", e3);
	}
	
	@Override
	public void render() {
		if(!isSpoken && !BlockLife.ExistsWorlds)
		{
			//Voice.speak(Voice.welcomeANDplaytutorial);
			isSpoken = true;
		}
		
		while(Keyboard.next())
			if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
				showESCmenu = !showESCmenu;
		
		world.render(showESCmenu);
		
		if(showESCmenu)
		{
			RenderSystem.renderColoredQuad(0, 0, BlockLife.DisplayWidth, BlockLife.DisplayHeight, 0f, 0f, 0f, 0.6f);
			backToGame.render();
			backToMenu.render();
			craftingRecipes.render();
		}
	}

	@Override
	public boolean init() {
		Block.LoadBlocks();
		
		world = new World(seed);
		
		BlockLife.BackgroundR = 0;
		BlockLife.BackgroundG = 0;
		BlockLife.BackgroundB = 0;
		
		return true;
	}

	@Override
	public boolean quit() {
		BlockLife.BackgroundR = 1;
		BlockLife.BackgroundG = 1;
		BlockLife.BackgroundB = 1;
		
		return true;
	}

}
